﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.订单一览新.订单预览新.model;
using DXApplicationTangche.UC.货号管理.service;
using DXApplicationTangche.UC.订单一览新.辅料.辅料下单;
using DXApplicationTangche.UC.订单一览新.辅料.service;
using DXApplicationTangche.UC.门店下单.DTO;
using DXApplicationTangche.service;
using DXApplicationTangche.UC.款式管理;
using DXApplicationTangche.UC.款式尺寸;
using mendian;
using DXApplicationTangche.DTO;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using DXApplicationTangche.UC.金蝶.dto;
using DXApplicationTangche.UC.金蝶;
using System.Text.RegularExpressions;

namespace DXApplicationTangche.UC.货号管理.销售款式
{
    public partial class Form样衣登记 : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        //uc
        //private XtraUserControl辅料选择 uC辅料 = new XtraUserControl辅料选择();
        //model
        private 订单预览新Model model;
        //变量
        private String barcode;
        private String saleInventoryMatialId;
        private Boolean if更新 = false;//是否为更新
        private DTO销售款式 dTO销售款式 = new DTO销售款式();//登记样衣返回值
        #region 构造函数
        public Form样衣登记()
        {
            InitializeComponent();
            this.barButtonItem更新样衣图片.Enabled = false;
        }
        /// <summary>
        /// 查看样衣一览的销售样衣右键查看登记信息进入（已登记过）
        /// </summary>
        /// <param name="dTO销售款式"></param>
        /// <param name="vO销售样衣"></param>
        public Form样衣登记(DTO销售款式 dTO销售款式,VO销售样衣 vO销售样衣)
        {
            InitializeComponent();
            this.if更新 = true;
            this.barcode = vO销售样衣.stockInventoryNo;
            this.saleInventoryMatialId = vO销售样衣.saleInventoryMatialId;
            this.selectOrderCommit();
            this.textBox成衣条码.Text = this.barcode;
            this.textBox成衣条码.ReadOnly = true;
            this.textBox单价.Text = vO销售样衣.salePrice.ToString();
            this.comboBox款式标签.Items.Clear();
            this.comboBox款式标签.Items.Add(dTO销售款式.styleTag);
            this.comboBox款式标签.SelectedIndex = 0;
            this.comboBox性别标签.Items.Clear();
            this.comboBox性别标签.Items.Add(dTO销售款式.styleSexFlag);
            this.comboBox性别标签.SelectedIndex = 0;
            this.pictureBox样衣图片.Image = vO销售样衣.sampleImage;
        }
        /// <summary>
        /// 查看更新样衣信息和样品一栏进入（已登记过）
        /// </summary>
        /// <param name="barcode"></param>
        /// <param name="salePrice"></param>
        /// <param name="dTO销售款式"></param>
        public Form样衣登记(DTO销售款式 dTO销售款式, SaleStyleSample saleStyleSample)
        {
            InitializeComponent();
            this.if更新 = true;
            this.barcode = saleStyleSample.stockInventoryNo;
            this.saleInventoryMatialId = saleStyleSample.saleInventoryMatialId;
            this.selectOrderCommit();
            this.textBox成衣条码.Text = this.barcode;
            this.textBox成衣条码.ReadOnly = true;
            this.textBox单价.Text = saleStyleSample.salePrice.ToString();
            this.comboBox款式标签.Items.Clear();
            this.comboBox款式标签.Items.Add(dTO销售款式.styleTag);
            this.comboBox款式标签.SelectedIndex = 0;
            this.comboBox性别标签.Items.Clear();
            this.comboBox性别标签.Items.Add(dTO销售款式.styleSexFlag);
            this.comboBox性别标签.SelectedIndex = 0;
            try
            {
                this.pictureBox样衣图片.Image = FileService.GetImageByUrl(URL.图片url + URL.销售样衣实例图片路径 + saleStyleSample.sampleImagePath);
            }
            catch
            {
                this.pictureBox样衣图片.Image = FunctionHelper.getSSHIRT();
            }
        }
        /// <summary>
        /// 样品订单一览进入，未登记过
        /// </summary>
        /// <param name="barcode"></param>
        public Form样衣登记(String barcode)
        {
            InitializeComponent();
            this.barButtonItem更新样衣图片.Enabled = false;
            this.barcode = barcode;
            this.textBox成衣条码.Text = this.barcode;
            this.selectOrderCommit();
        }
        #endregion
        private void Form样衣登记_Load(object sender, EventArgs e)
        {
            this.ActiveControl = this.textBox成衣条码;
            this.textBox单价.ReadOnly = true;
            this.comboBox款式标签.Enabled = false;
            this.comboBox性别标签.Enabled = false;
        }
        private void textBox成衣条码_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyValue == 13)
            {
                if (!String.IsNullOrWhiteSpace(this.textBox成衣条码.Text.Trim())
                    &&货号管理Service.barcodeExisted(this.textBox成衣条码.Text.Trim()))
                {
                    this.barcode = this.textBox成衣条码.Text.Trim();
                    this.selectOrderCommit();
                    this.textBox单价.ReadOnly = false;
                    this.comboBox款式标签.Enabled = true;
                    this.comboBox性别标签.Enabled = true;
                }
                else
                {
                    this.textBox单价.ReadOnly = true;
                    this.comboBox款式标签.Enabled = false;
                    this.comboBox性别标签.Enabled = false;
                    MessageBox.Show("请输入正确成衣条码");
                }
            }
        }
        private void selectOrderCommit()
        {
            try
            {
                this.model = new 订单预览新Model(this.barcode, Enum订单查询.按照水洗标查询订单);
            }
            catch (Exception)
            {
                MessageBox.Show("请正确扫描成衣条码");
                return;
            }
            信息呈现();
        }
        private void 信息呈现()
        {
            //客户相关
            this.textBox客户名.Text = this.model.OrderCommitDTO返回.customerName;
            this.textBox电话.Text = this.model.OrderCommitDTO返回.mobile;
            this.textBox收货人.Text = this.model.OrderCommitDTO返回.consignee;
            this.model.OrderCommitDTO返回.addressProvince = this.model.OrderCommitDTO返回.addressProvince == null ? "" : this.model.OrderCommitDTO返回.addressProvince;
            this.model.OrderCommitDTO返回.addressCity = this.model.OrderCommitDTO返回.addressCity == null ? "" : this.model.OrderCommitDTO返回.addressCity;
            this.model.OrderCommitDTO返回.addressDistrict = this.model.OrderCommitDTO返回.addressDistrict == null ? "" : this.model.OrderCommitDTO返回.addressDistrict;
            this.model.OrderCommitDTO返回.addressDetail = this.model.OrderCommitDTO返回.addressDetail == null ? "" : this.model.OrderCommitDTO返回.addressDetail;
            this.model.OrderCommitDTO返回.zipCode = this.model.OrderCommitDTO返回.zipCode == null ? "" : this.model.OrderCommitDTO返回.zipCode;
            this.textBox发运信息地址.Text =
                this.model.OrderCommitDTO返回.addressProvince +
                this.model.OrderCommitDTO返回.addressCity +
                this.model.OrderCommitDTO返回.addressDistrict +
                this.model.OrderCommitDTO返回.addressDetail +
                this.model.OrderCommitDTO返回.zipCode;
            //显示成衣码查询出内容，无参考意义的进行清空
            this.model.buildOrderCommit();
            //款式
            this.gridControl款式.DataSource = this.model.款式DTOs;
            //设计点
            this.gridControl设计点.DataSource = this.model.设计点DTOs;
            //面料
            this.gridControl面料.DataSource = this.model.面料DTOs;
            //尺寸
            try { this.comboBox尺码组.Text 
                    = this.model.OrderCommitDTO返回.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
            this.treeList尺寸.DataSource = this.model.尺寸.measureItemDTOS;
            // 辅料uc
            //this.uC辅料 =
            //    new XtraUserControl辅料选择(
            //        new 标识StyleAccessoryDTO(false, this.model.OrderCommitDTO返回.styleAccessoryDTOs),
            //        this.model.OrderCommitDTO返回.materialCode
            //        );
            //this.uC辅料.Dock = DockStyle.Fill;
            //this.panel辅料.Controls.Clear();
            //this.panel辅料.Controls.Add(this.uC辅料);
        }

        private void barButtonItem更新辅料_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //this.model.saveAccessory(this.uC辅料.build().styleAccessoryDTOs);
            //MessageBox.Show("操作完成");
        }

        private void barButtonItem确认样衣上新_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //判断样衣登记基本信息是否填写
            if (String.IsNullOrEmpty(this.textBox成衣条码.Text.Trim())
                || String.IsNullOrEmpty(this.comboBox款式标签.Text.Trim())
                || String.IsNullOrEmpty(this.comboBox性别标签.Text.Trim())
                || String.IsNullOrEmpty(this.textBox单价.Text.Trim()))
            {
                MessageBox.Show("请将样衣基本信息填写完整");
                return;
            }
            //判断BOM是否创建
            if (!货号管理Service.sampleGarmentMaterialExited(this.model.OrderCommitDTO返回.styleNameCn, this.model.OrderCommitDTO返回.materialCode))
            {
                MessageBox.Show("样衣BOM清单未登记，请尽快补充");
            }
            try
            {
                if (this.if更新)
                {
                    this.dTO销售款式 =
                        货号管理Service.createInvetoryByBarcode(
                            this.barcode,
                            this.comboBox款式标签.Text.Trim(),
                            this.comboBox性别标签.Text.Trim(),
                            Convert.ToDouble(this.textBox单价.Text.Trim())
                            );
                }
                else
                {
                    OpenFileDialog ofd = new OpenFileDialog();
                    ofd.Filter = "gif,jpg,png|*.gif;*.jpg;*.png;*.jpeg";
                    ofd.Title = "请选择图片文件";
                    if (ofd.ShowDialog() == DialogResult.OK)
                    {
                        this.dTO销售款式 =
                            货号管理Service.postCreateInvetoryByBarcode(
                                this.barcode,
                                this.comboBox款式标签.Text.Trim(),
                                this.comboBox性别标签.Text.Trim(),
                                Convert.ToDouble(this.textBox单价.Text.Trim()),
                                ofd
                                );
                        new Form销售款式维护确认(this.dTO销售款式).ShowDialog();
                    }
                }
                ////样衣BOM保存后更新
                //String id = Regex.Replace(this.model.OrderCommitDTO返回.styleNameCn, " .*", "") + "_" + this.model.OrderCommitDTO返回.materialCode;
                //货号管理Service.buildSaleSampleCreateBom(id);
                MessageBox.Show("操作完成");
            }
            catch (Exception)
            {
                MessageBox.Show("操作失败");
            }
        }
        
        private void barButtonItem更新样衣图片_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "gif,jpg,png|*.gif;*.jpg;*.png;*.jpeg";
            ofd.Title = "请选择图片文件";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                String str = 货号管理Service.saveSaleInventoryMatialId(saleInventoryMatialId, ofd);
                if (!String.IsNullOrWhiteSpace(str))
                {
                    try
                    {
                        this.pictureBox样衣图片.Image = FileService.GetImageByUrl(URL.图片url + URL.销售样衣实例图片路径 + str);
                    }
                    catch
                    {
                        this.pictureBox样衣图片.Image = FunctionHelper.getSSHIRT();
                    }
                    MessageBox.Show("操作完成");
                }
            }
        }

        private void textBox单价_KeyPress(object sender, KeyPressEventArgs e)
        {
            //允许输入数字、小数点、删除键和负号  
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.'))
            {
                //MessageBox.Show("请输入正确的数字");
                this.textBox单价.Text = "";
                e.Handled = true;
            }
            if (e.KeyChar == (char)('-'))
            {
                if (textBox单价.Text != "")
                {
                    //MessageBox.Show("请输入正确的数字");
                    this.textBox单价.Text = "";
                    e.Handled = true;
                }
            }
            //小数点只能输入一次  
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf('.') != -1)
            {
                //MessageBox.Show("请输入正确的数字");
                this.textBox单价.Text = "";
                e.Handled = true;
            }
            //第一位不能为小数点  
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
            {
                //MessageBox.Show("请输入正确的数字");
                this.textBox单价.Text = "";
                e.Handled = true;
            }
            //第一位是0，第二位必须为小数点  
            if (e.KeyChar != (char)('.') && ((TextBox)sender).Text == "0")
            {
                //MessageBox.Show("请输入正确的数字");
                this.textBox单价.Text = "";
                e.Handled = true;
            }
            //第一位是负号，第二位不能为小数点  
            if (((TextBox)sender).Text == "-" && e.KeyChar == (char)('.'))
            {
                //MessageBox.Show("请输入正确的数字");
                this.textBox单价.Text = "";
                e.Handled = true;
            }
        }

        private void barButtonItem保存BOM_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if(String.IsNullOrEmpty(this.barcode))
            {
                MessageBox.Show("请正确扫描成衣条码");
                return;
            }
            new Form金蝶EBOM修改(this.model.OrderCommitDTO返回.styleNameCn, this.model.OrderCommitDTO返回.materialCode).ShowDialog();
        }
    }
}